<div class="content">
	<div class="tab_bar">
		<ul>
			<li class="tab_index" onclick="setTabContent(this);">新建文章</li>
		</ul>

		<#if Session.userGroupId == 1>
			<button onclick="submit(this)" class="btn_tabbar submit_it" return="article/list.do?currentPageNo=1">发 布</button>
		</#if>
		<button onclick="save(this)" class="btn_tabbar submit_it" return="article/list.do?currentPageNo=1">保存草稿</button>
	</div>


	<form id="myForm" action="article/save.do" method="post" class="form-horizontal" onsubmit="return false;">

	<input id="status" name="status" value="0" type="hidden" />
	<div class="tab_content">
		<div class="tab_item tab_show">

			<div class="control-group">
				<label class="control-label">所属栏目：</label>
				<div class="controls">
					<select name="cid">
						<@channelTree id = 0 node = -1/>
					</select>
					<div class="help-block"></div>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">所属分类：</label>
				<div class="controls">
					<select name="did">
						<option value="-1"> -------- 没有分类 -------- </option>
						<@categoryTree id = 0 node = -1/>
					</select>
					<div class="help-block"></div>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">文章标题：</label>
				<div class="controls">
					<input id="title" name="title" type="text" placeholder="请输入文章标题" />
					<div class="help-block"></div>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">文章图标：</label>
				<div class="controls">
					<input id="articleIcon" class="input_for_add" name="icon" type="text" />
					<button type="button" onclick="uploadImage()">上传图片</button>
					<div class="help-block"></div>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">内容类型：</label>
				<div class="controls">
					<input id="statistics_1" name="type" type="radio" value="0" checked="checked" /><label for="statistics_1">&nbsp;HTML&nbsp; &nbsp;</label>
					<input id="statistics_2" name="type" type="radio" value="1" /><label for="statistics_2">&nbsp;text/markdown&nbsp; &nbsp;</label>

					<div class="help-block">切换类型将会清空内容哦</div>
				</div>
			</div>

			<div class="control-group">
				<script type="text/plain" id="mushroomeditor" name="content"  style="height:350px;"></script>
			</div>


			<div class="control-group">
				<label class="control-label">文章关键字：</label>
				<div class="controls">
					<input id="keywords" class="input_for_add" name="keywords" type="text" />
					<div class="help-block"> </div>
				</div>
			</div>

			<div class="control-group">
				<label class="control-label">文章描述：</label>
				<div class="controls">
					<textarea class="textarea_for_add" name="description"></textarea>
					<div class="help-block"> </div>
				</div>
			</div>


			<div class="control-group">
				<label class="control-label">文章作者：</label>
				<div class="controls">
					<input class="input_for_add" name="author" type="text" value="${loginusername!}" />
					<div class="help-block"> </div>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">原文地址：</label>
				<div class="controls">
					<input class="input_for_add" name="source" type="text" value="" />
					<div class="help-block"> </div>
				</div>
			</div>

		</div>
	</div>
	</form>
</div>
<script type="text/javascript">

//初始化UE编辑器
$(function(){
	$("input[name='type']").click(function(){
 		editor.execCommand( 'source');
		if($(this).val() == 1){
 	 		editor.setDisabled();
		}else{ 
			editor.setEnabled();
		}
	 	editor.setContent('');
	});
	
	
	
	if(window.editor != null){
		window.editor.destroy();
	}

    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    window.editor = UE.getEditor('mushroomeditor');
 	
 	window.editor.ready(function(){
 		$("#title").focus();
 		
 		var status = editor.queryCommandState( 'source' );
 		if(status != 1){
 			
 		}

 		
 	});
 	 
});


function submit(obj){
	$("#status").val("1");
	window.editor.sync();//手动提交需要手动同步编辑器数据
	
	submitActionForm(obj); 
	
}


function save(obj){
	$("#status").val("0");
	window.editor.sync();//手动提交需要手动同步编辑器数据
	
	submitActionForm(obj); 
	
}

 
	$('#myForm').validator({
	    // 传入字段集合
	    fields: {
	    	"title": "required" 
	    }
	}).on("click", ".submit_it", function(){
	   // $('#myForm').trigger("submit");
	    
	 	window.editor.sync();//手动提交需要手动同步编辑器数据
		submitActionForm(obj); 
	    
	});
	
	
 



/**
 * 关键字获取接口
 * */
function keyword(){
	var title = $("#title").val();
	var content = window.editor.getContent(); 
	$.ajax({
		url:"../keywords.do",
		type:"POST",
		data: "title="+title+"&content="+content,
		dataType:"json",
		success:function(data){
			$("#keywords").val(data);
		}
	});
	
}



var _editor = UE.getEditor('upload_ue');
_editor.ready(function () {
    console.log("ueditor success!")
    //设置编辑器不可用
    _editor.setDisabled();
    //隐藏编辑器，因为不会用到这个编辑器实例，所以要隐藏
    _editor.hide();
    //侦听图片上传
    _editor.addListener('beforeInsertImage', function (t, arg) {
        console.log(arg);
    })
    //侦听文件上传
    _editor.addListener('afterUpfile', function (t, arg) {
        console.log(arg);
        $("#articleIcon").attr("value", arg[0].src);
    })
});

function uploadImage(){
    var myImage = _editor.getDialog("insertimage");
    myImage.open();
}
</script>

<#macro categoryTree id node>
	<#local temp = node + 1 />
	<#list categorys as x>
	  	<#if id = x.pid>
	  	<option value="${x.id?c}"><#list 0..temp as i> &nbsp; &nbsp; </#list>├ ${x.name!}</option>
	    <@categoryTree id = x.id node=temp/>
      	</#if>
	</#list> 
</#macro>

<#macro channelTree id node>
	<#local temp = node + 1 />
	<#list channels as comment>
		<#if id = comment.pid>
			<option value="${comment.id?c}"><#list 0..temp as x> &nbsp; &nbsp; </#list>├ ${comment.name!}</option>
			<@channelTree id = comment.id node=temp/>
		</#if>
	</#list>
</#macro>